var name = "g_AneHekBase"
var test = false
var data = {"leader.three":true}
var setting = {}
var file = File.new()
var ctrl = null
var eventbuff = null
var utils = null

signal onPickCha    #选角色界面时发射信号
signal onPickItem   #选道具界面时发射信号
signal onNewGame    #初次进入游戏界面时发射信号
signal onLoadGame   #读取进入游戏界面时发射信号
signal onSaveGame   #退回标题页面时发射信号
signal onStartGame #onNewGame + onLoadGame
signal baseInit
func getSetting(name, default = null):
	if setting == null:
		setting = {}
	var s = setting.get(name)
	if s == null && default != null:
		setSetting(name, default)
		s = default
	return s

func setSetting(name, value, save=true):
	setting[name] = value
	if save:
		saveSetting()

func getData(name,default = null):
	if data == null:
		data = {"leader.three":true}
	#print(data)
	var d = data.get(name)
	#print(d)
	if d == null && default != null:
		setData(name,default)
		d = default
	return d

func setData(name,value,save = true):
	data[name] = value
	if save:
		saveData()

func _init():
	call_deferred("baseInit")
	loadData()
	loadSetting()
	yield(sys.get_tree().create_timer(0.2), "timeout")
	sys.get_node("/root/Control/Panel2/Panel/Button").connect("pressed",self,"gameInit",["gameSet",true])
	sys.get_node("/root/Control/Panel2/Panel/jiXuBtn").connect("pressed",self,"gameInit",["gameStart",false])
	

func baseInit():
	ctrl = globalData.infoDs["g_AneHek"]
	utils = globalData.infoDs["g_AneHekUtils"]
	#print(ctrl)
	#print(eventbuff)
	

func gameInit(name, sel):
	if name == "gameSet":
		call_deferred("gameSet")
	elif name == "gameStart":
		call_deferred("gameStart",sel)

func gameSet():
	sys.get_node("/root/topUi").get_children()[-1].connect("tree_exited",self,"gamePickCha")

func gamePickCha():
	if sys.has_node("../main"):
		sys.get_node("/root/topUi").find_node("?msgBaseX*",false,false).get_node("btn_start").connect("button_down",self,"gamePickItem")
		emit_signal("onPickCha")

func gamePickItem():
	sys.get_node("/root/topUi/msgBaseX").get_node("btn_start").connect("button_down",self,"gameInit",["gameStart",true])
	emit_signal("onPickItem")
	#for i in chaData.rndPool.items:
	#	print(i[0].id + "   ",i[1])
	#chaData.rndPool.items.append([sys.main.newChara("cex___AneHekA21"),1])

func gameStart(sel):
	loadSetting()
	emit_signal("onStartGame")
	if sel:
		initData()
		emit_signal("onNewGame")
	else:
		loadData()
		emit_signal("onLoadGame")
	sys.main.connect("tree_exited",self,"gameExit")
	
func gameExit():
	saveData()
	emit_signal("onSaveGame")
	_init()

func initData():
	data = {"leader.three":true,"x1":data.x1}
	if test: print("Data:Init %s" % name)

func loadData():
	if file.file_exists("user://data1/AHTDData.save"):
		file.open("user://data1/AHTDData.save", File.READ)
		if file.get_len() > 0:
			data = parse_json(file.get_line())
		file.close()
		#print(data)
		#if data.pt != null:
		#	ctrl.pt = data.pt
		# if data.difficult != null:
		#     ctrl.difficult = data.difficult

func saveData():
	#data["pt"] = ctrl.pt
	# data["difficult"] = ctrl.difficult
	data["spoint"] = utils.spoint
	data["jt"] = utils.jt
	data["mfq"] = utils.mfq
	data["sf"] = utils.sf
	data["sp"] = utils.sp
	data["txz"] = utils.txz
	data["ypz"] = utils.ypz
	file.open("user://data1/AHTDData.save", File.WRITE)
	file.store_line(to_json(data))
	file.close()
	saveSetting()

func saveSetting():
	# print("保存碧蓝航线相关设置")
	# setting["difficult"] = ctrl.difficult
	file.open("user://data1/AHTDSetting.save", File.WRITE)
	file.store_line(to_json(setting))
	file.close()

func loadSetting():
	if file.file_exists("user://data1/AHTDSetting.save"):
		file.open("user://data1/AHTDSetting.save", File.READ)
		if file.get_len() > 0:
			setting = parse_json(file.get_line())
		file.close()
		#print(setting)
		# if setting.difficult != null:
		#     ctrl.difficult = int(setting.difficult)


func loadImg(path,imgPath) -> ImageTexture:
	var im = Image.new()
	var realPath = "%s/%s" % [path,imgPath]
	#print(realPath)
	if not file.file_exists(realPath):return null
	im.load(realPath)
	var imt = ImageTexture.new()
	imt.create_from_image(im)
	return imt

func find_file(path:String, filter:Array, flip:bool = false) -> Array:
	var file_list = []
	var dir = Directory.new()
	if dir.open(path) == OK:
		dir.list_dir_begin(true)
		var file_name = dir.get_next()
		while file_name != "":
			if dir.current_is_dir():
				file_list += find_file(path + "/" + file_name, filter, flip)
			else:
				if flip:
					if not file_name.split(".")[-1] in filter:
						file_list.append(path + "/" + file_name)
				else:
					if file_name.split(".")[-1] in filter:
						file_list.append(path + "/" + file_name)
			file_name = dir.get_next()
	else:
		print("错误：查找路径"+ path +"时出错")
	return file_list

# .rndPool.items
# .infoDs
# sys
# config
# chaData
# itemData
# guanKaData
# jinJieData
# talentData
# globalData
# topUi
# audio
# godotsteam
# Control

# .import
# Images.xcassets
# core
# ex
# godotSteam
# mods
# res
# ui
# control.gd.remap
# control.gdc
# control.tscn
# default_bus_layout.tres
# default_env.tres
# godotsteam.gdnlib
# ico.png
# ico.png.import
# ico16.ico
# ico32.ico
# main.gd.remap
# main.gdc
# main.tscn
# project.binary
# test.tscn